package com.youbug.mall.member_ums.service.impl;

import com.youbug.mall.member_ums.entity.UmsMemberLevel;
import com.youbug.mall.member_ums.mapper.UmsMemberLevelMapper;
import com.youbug.mall.member_ums.service.IUmsMemberLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员等级 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2024-04-14
 */
@Service
public class UmsMemberLevelServiceImpl extends ServiceImpl<UmsMemberLevelMapper, UmsMemberLevel> implements IUmsMemberLevelService {

        @Override
        public IPage<UmsMemberLevel> getUmsMemberLevelPage(QueryVo<UmsMemberLevel> queryVo) {

        Page<UmsMemberLevel> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<UmsMemberLevel> wrapper = new QueryWrapper<>();

        UmsMemberLevel umsMemberLevel = queryVo.getPayload();

        if(!ObjectUtils.isEmpty(umsMemberLevel)){
            if(StringUtils.hasText(umsMemberLevel.getName())){
                wrapper.eq("name", umsMemberLevel.getName());
            }
            if(umsMemberLevel.getPriviledgeMemberPrice()!=null){
                wrapper.eq("priviledge_member_price", umsMemberLevel.getPriviledgeMemberPrice());
            }
            if(umsMemberLevel.getDefaultStatus()!=null){
                wrapper.eq("default_status", umsMemberLevel.getDefaultStatus());
            }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
        wrapper.orderByDesc("create_time");

        Page<UmsMemberLevel> page = this.page(paramPage, wrapper);

        return page;
        }

    @Override
    public UmsMemberLevel getDefaultLevel() {

        UmsMemberLevel memberLevel = this.getOne(new QueryWrapper<UmsMemberLevel>().eq("default_status", "1"));

        return memberLevel;
    }

}
